﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.WinForm.ControlFlash
{
    public class DataGridViewFlashItem : FlashItemBase
    {
        private DataGridView dataGrid = null;

        public DataGridViewFlashItem(DataGridView dg, int rowIndex, DataGridViewColumn column)
            : base(dg)
        {
            if (dg == null)
            {
                throw new NullReferenceException("指定DataGridView不能为空");
            }

            if (Column != null)
            {
                if (dg.Columns.Contains(column))
                {
                    throw new Exception("指定列必须包含在指定的DataGridView中");
                }
            }

            if (column==null)
            {
                OriginalColor = dataGrid.GetCellBackColor(rowIndex, 0);
            }
            else
            {
                OriginalColor = dataGrid.GetCellBackColor(rowIndex, column.Index);
            }

            RowIndex = rowIndex;
            Column = column;
            SourceControl = dg;
            dataGrid = dg;
        }

        public int RowIndex { get; private set; }

        public DataGridViewColumn Column { get; private set; }

        protected override void SetColor(Color color)
        {
            if (dataGrid.Rows.Count < this.RowIndex)
            {
                return;
            }

            //如果当前颜色不需要改变，则跳出，增加这个检查，减少控件的刷新
            if (this.CurrentColorType==FlashColorType.FlashColor && color==this.FlashColor)
            {
                return;
            }
            else if(this.CurrentColorType==FlashColorType.OriginalColor && color==this.OriginalColor)
            {
                return;
            }

            if (this.Column != null)
            {
                dataGrid.SetCellBackColor(RowIndex, Column.Index, color);
            }
            else
            {
                dataGrid.SetRowBackColor(RowIndex, color);
            }


        }

        public bool Equals(DataGridViewFlashItem other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            if (this.Column == null || other.Column == null)
            {
                return Equals(other.dataGrid, dataGrid) && other.RowIndex == RowIndex;
            }
            else
            {
                return Equals(other.dataGrid, dataGrid) && other.RowIndex == RowIndex && Equals(other.Column, Column);
            }
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(DataGridViewFlashItem)) return false;
            return Equals((DataGridViewFlashItem)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (dataGrid != null ? dataGrid.GetHashCode() : 0);
                result = (result * 397) ^ RowIndex;
                result = (result * 397) ^ (Column != null ? Column.GetHashCode() : 0);
                return result;
            }
        }
    }
}
